home *** CD-ROM | disk | FTP | other *** search
/ Grapevine 18 / Grapevine 18 (Disk 2 of 3).adf / LookHere / AREXXtut.lha / Grapevine / ondisk / gv_convert.rexx
Encoding:
OS/2 REXX Batch file  |  1980-01-30  |  8.0 KB  |  286 lines

  1. /* Grapevine article conversion AREXX script */
  2. /* R.J.Osbaldeston          v1.00, July 1993 */
  3.  
  4. arg filename dst_filename options
  5.  
  6. /* ++ PROCESS PARAMETERS ++ */
  7. If arg()=0 Then Do
  8.    Say 'Please enter the article filename: '
  9.    pull filename
  10.    End
  11. If dst_filename == '' Then Do
  12.    dst_filename = filename||'.cnv'
  13.    End
  14.  
  15. /* ++ Open GV article for read, and an output file for writing ++ */
  16. success = open('src_handle',filename,'r')
  17. If success = 0 Then Do
  18.    Say 'Unable to open that Article!'
  19.    exit
  20.    End
  21. Else Do
  22.    src_line = Readln('src_handle')
  23.                                        /* Do a quick check on the file type */
  24.    If Left(src_line,10) ~= '----------' Then Do
  25.       Say 'ERROR: File Format, Not A Recognised Grapvine Article.'
  26.       exit
  27.       End
  28.    End
  29.  
  30. success = open('dst_handle',dst_filename,'w')
  31. If success = 0 Then Do
  32.    Say 'Unable to open article destination.'
  33.    exit
  34.    End
  35.  
  36. do_title = 1                              /* check for command line options */
  37. If index(options,'/NO_TITLE')>0 Then do_title=0
  38.  
  39.  
  40. /* ++ INITIALIZE VARIABLES ++ */
  41. page_brk = copies('-',80)                            /* page break constant */
  42. lf_token1 = 'FF'x                           /* alternating linefeed token 1 */
  43. lf_token2 = 'FE'x                           /* alternating linefeed token 2 */
  44. lf_toggle_known = 0
  45. lf_toggle_tmp = 0
  46. pages_read = 0                                 /* number of pages processed */
  47. lf_toggle_col1 = 0              /* flags used to keep track of linefeeds .. */
  48. lf_toggle_col2 = 0              /* .. while writing to destination.         */
  49.  
  50.  
  51. call time 'R'                             /* Reset timer, start timing now! */
  52. Say 'Working'
  53.  
  54. call process_header                 /* Process the Grapevine article header */
  55.  
  56.  
  57. Do While ~eof('src_handle')
  58.  
  59.    first_col_buffer  = ''
  60.    second_col_buffer = ''
  61.    lf_toggle_known = 0
  62.    lf_toggle_tmp = 0
  63.    lines_read = 0
  64.  
  65.    If (do_title & pages_read=0) Then call process_title
  66.  
  67.                    /* process a text section, until End of the current page 
  68.                       or the End of the source file, or buffer overflow.    */
  69.  
  70.    /* ++ PROCESS A PAGE ++ */
  71.    Do While (~eof('src_handle'))
  72.  
  73.       src_line = Trim(Readln('src_handle'))
  74.  
  75.       If src_line = page_brk Then Leave
  76.  
  77. /* --- If Substr(src_line,39,2) ~= '  ' Then say '*' src_line --- */
  78.  
  79.       /* ++ PROCESS FIRST COLUMN ++ */
  80.       column_txt = Trim(Left(src_line,40))
  81.  
  82.       If length(column_txt)=0 Then 
  83.          Do
  84.          first_col_buffer = first_col_buffer||'0A'x
  85.          If lf_toggle_col1 Then first_col_buffer = first_col_buffer||'0A'x
  86.          lf_toggle_col1 = 0
  87.          End
  88.       Else
  89.          Do 
  90.          If lf_toggle_col1 Then
  91.             first_col_buffer = first_col_buffer||column_txt||'0A'x
  92.          Else
  93.             first_col_buffer = first_col_buffer||column_txt||' '
  94.          lf_toggle_col1 = ~lf_toggle_col1
  95.          End
  96.  
  97.  
  98.       /* ++ PROCESS SECOND COLUMN ++ */
  99.       column_txt=Delstr(src_line,1,40)
  100.  
  101.       If Words(column_txt)=0 Then
  102.          Do
  103.          second_col_buffer = second_col_buffer||'0A'x
  104.  
  105.          If lf_toggle_known Then
  106.             Do
  107.              If lf_toggle_col2 Then second_col_buffer = second_col_buffer||'0A'x
  108.             End
  109.          Else
  110.             Do
  111.                        /* .. PROBLEM, lf_toggle not known at this point,and 
  112.                           this will occationally cause an extra linefeed    */
  113.  
  114.              second_col_buffer = second_col_buffer||'0A'x
  115.              lf_toggle_known = 1
  116.             End
  117.          lf_toggle_col2 = 0
  118.          End
  119.       Else 
  120.          Do
  121.          If lf_toggle_known Then
  122.             Do
  123.             If lf_toggle_col2 Then
  124.                second_col_buffer = second_col_buffer||column_txt||'0A'x
  125.             Else
  126.                second_col_buffer = second_col_buffer||column_txt||' '
  127.             lf_toggle_col2= ~lf_toggle_col2
  128.             End
  129.          Else
  130.             Do
  131.             If lf_toggle_tmp Then
  132.                second_col_buffer = second_col_buffer||column_txt||lf_token2
  133.             Else
  134.                second_col_buffer = second_col_buffer||column_txt||lf_token1
  135.             lf_toggle_tmp = ~lf_toggle_tmp
  136.             End
  137.          End
  138.  
  139.       lines_read = lines_read + 1
  140.       End
  141.  
  142.  
  143.    /* +++ PROCESS TEMP LF_TOKEN REPLACEMENT, FOR SECOND COLUMN +++ */
  144.    If lf_toggle_col1 Then
  145.    Do
  146.       ctrl_command1 = '0A'x
  147.       ctrl_command2 = ' '
  148.    End
  149.    Else
  150.    Do
  151.       ctrl_command1 = ' '
  152.       ctrl_command2 = '0A'x
  153.    End
  154.  
  155.    lf_pos = pos(lf_token1,second_col_buffer)
  156.    Do While lf_pos ~= 0
  157.       second_col_buffer = overlay(ctrl_command1,second_col_buffer,lf_pos)
  158.       lf_pos = pos(lf_token1,second_col_buffer,lf_pos)
  159.    End
  160.  
  161.    lf_pos = pos(lf_token2,second_col_buffer)
  162.    Do While lf_pos ~= 0
  163.       second_col_buffer = overlay(ctrl_command2,second_col_buffer,lf_pos)
  164.       lf_pos = pos(lf_token2,second_col_buffer,lf_pos)
  165.    End
  166.  
  167.  
  168.    /* ++ WRITE PROCESSED PAGE, FIRST COLUMN PLUS SECOND ++ */
  169.    Writech('dst_handle',first_col_buffer||second_col_buffer);
  170.  
  171.  
  172.    /* ++ HANDLE NEXT PAGE LF TOGGLES ++ */
  173.    If ~lf_toggle_known Then
  174.    Do
  175.       If lf_toggle_tmp Then lf_toggle_col1 = 1 Else lf_toggle_col1 = 0
  176.    End
  177.    Else 
  178.    Do
  179.       lf_toggle_col1 = lf_toggle_col2
  180.    End
  181.  
  182.    pages_read = pages_read + 1
  183.    Say '.'                                      /* keep the user interested */
  184.  
  185.                                  /* Check wether this is the End of article */
  186.    If lines_read < 2 Then Leave
  187.    End                                         /* ..Otherwise continue loop */
  188.  
  189. Close('src_handle')                    /* close files, free memory and exit */
  190. Close('dst_handle')
  191.  
  192. Say 'Finished. <' pages_read 'Pages, Time taken' time('E')||'s >'
  193. Say ''
  194. Exit
  195.  
  196. /* ------------------------------------------------------------------------ */
  197.  
  198. process_header: PROCEDURE
  199.  
  200.    prev_pos = Seek('src_handle',0,'B')
  201.    header_line1 = Readln('src_handle')
  202.  
  203.    Writeln('dst_handle',header_line1)
  204.  
  205.    header_line2 = Readln('src_handle')
  206.    Do while (Left(header_line2,10) ~= '----------')
  207.       Writeln('dst_handle',header_line2)
  208.       prev_pos = Seek('src_handle',0,'C')
  209.       header_line2 = Readln('src_handle')
  210.       End
  211.  
  212.    Writeln('dst_handle',header_line1)
  213.  
  214.  
  215.              /* Often there is no LF after the End of the header and the
  216.                 start of the title, check for this and reset the file
  217.                 pointer appropriatley                                      */
  218.  
  219.    If length(header_line2) > 80 Then Do
  220.       curr_pos = Seek('src_handle',prev_pos,'B')
  221.       header_line2 = Readch('src_handle',80)
  222.       End
  223.  
  224. Return(1)
  225.  
  226. /* ------------------------------------------------------------------------ */
  227.  
  228. process_title: PROCEDURE EXPOSE lines_read lines_written do_title
  229.  
  230. prev_pos = Seek('src_handle',0,'C')
  231. title_line = Readln('src_handle')
  232. lines_read = lines_read + 1
  233.  
  234. Do While (lines_read<8 & ,
  235.           (Words(title_line)=0 |,
  236.            Substr(title_line,39,2) ~= '  ' |,
  237.            check_leading(title_line) |,
  238.            check_upper() |,
  239.            check_dspace()))
  240.  
  241.    If length(title_line)<77 Then
  242.       Do
  243.         title_line = Strip(title_line,'B')
  244.         Writeln('dst_handle', Centre(title_line,77))
  245.       End
  246.    Else
  247.         Writeln('dst_handle',title_line)
  248.  
  249.    prev_pos = Seek('src_handle',0,'C')
  250.    title_line = Readln('src_handle')
  251.    lines_read = lines_read + 1
  252.    End
  253.  
  254. curr_pos = Seek('src_handle',prev_pos,'B')
  255. lines_read = lines_read - 1
  256.  
  257. Return(1)
  258.  
  259. /* ------------------------------------------------------------------------ */
  260.  
  261. check_dspace: EXPOSE title_line
  262. /* check wether characters are Doubled spaced */
  263. If Words(title_line)=0 Then Return(0)
  264. Do word_loop=1 To Words(title_line)
  265.    If WordLength(title_line, word_loop) ~= 1 Then Return(0)
  266.    End
  267. Return(1)
  268.  
  269. check_upper: EXPOSE title_line
  270. /* check wether characters are all upper case */
  271. If Words(title_line)=0 Then Return(0)
  272. Do word_loop=1 To Length(title_line)
  273.    If Substr(title_line,word_loop,1) >= 'a' &,
  274.       Substr(title_line,word_loop,1) <= 'z' Then Return(0)
  275.    End
  276. Return(1)
  277.  
  278.  
  279. /* ------------------------------------------------------------------------ */
  280.  
  281. check_leading:
  282. arg line_text
  283. /* check for leading spaces in line */
  284. If WordIndex(line_text,1) > 1 Then Return(1)
  285. Return(0)
  286.